﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam
{
    public partial class FormThongKe : Form
    {
        private CXulyHoaDon xuly_hoadon;
        private CXulyPhieuNhap xuly_phieunhap;
        private CXulyPhieuXuat xuly_phieuxuat;

        public FormThongKe()
        {
            InitializeComponent();
        }

        private void FormThongKe_Load(object sender, EventArgs e)
        {
            xuly_hoadon = new CXulyHoaDon();
            xuly_phieunhap = new CXulyPhieuNhap();
            xuly_phieuxuat = new CXulyPhieuXuat();

            loadcbb1();
            loadcbb2();
            loadcbb3();
            loadcbb4();
            loadcbb5();
            loadcbbNam();

            ThongKeSachNhap();
            ThongKeSachBan();
            ThongKeSachXuat();
            ThongKeHoaDon();
            ThongKePhieuNhap();

            if (cbbNam.Items.Count > 0 && cbbNam.Text != "")
                dodulieudgv(cbbNam.Text);
        }


        private void ThongKeSachNhap()
        {
            dgvNhapSach.Rows.Clear();
            if (cbb1.Items.Count > 0 && cbb1.Text != "")
            {
                List<PhieuNhap> dsphieu = new List<PhieuNhap>();
                DateTime dt = Convert.ToDateTime(cbb1.Text);

                foreach (PhieuNhap p in xuly_phieunhap.getDsPhieuNhap())
                    if (p.PhieuNhapDate.Value.Month == dt.Month && p.PhieuNhapDate.Value.Year == dt.Year)
                        dsphieu.Add(p);

                XulySachNhap(dsphieu);
            }
        }

        private void ThongKeSachBan()
        {
            dgvBanSach.Rows.Clear();
            if (cbb2.Items.Count > 0 && cbb2.Text != "")
            {
                List<HoaDon> dshd = new List<HoaDon>();
                DateTime dt = DateTime.Parse(cbb2.Text);

                foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                    if (hd.HoaDonDate.Value.Month == dt.Month && hd.HoaDonDate.Value.Year == dt.Year)
                        dshd.Add(hd);

                XulySachBan(dshd);
            }
        }

        private void ThongKeSachXuat()
        {
            dgvXuatSach.Rows.Clear();
            if (cbb3.Items.Count > 0 && cbb3.Text != "")
            {
                List<PhieuXuat> dspx = new List<PhieuXuat>();
                DateTime dt = DateTime.Parse(cbb3.Text);

                foreach (PhieuXuat px in xuly_phieuxuat.getDSPhieuXuat())
                    if (px.PhieuXuatDate.Value.Month == dt.Month && px.PhieuXuatDate.Value.Year == dt.Year)
                        dspx.Add(px);

                XulySachXuat(dspx);
            }
        }

        private void ThongKeHoaDon()
        {
            dgvListHD.Rows.Clear();
            if (cbb4.Items.Count > 0 && cbb4.Text != "")
            {
                DateTime dt = DateTime.Parse(cbb4.Text);
                double sum = 0;

                foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                    if (hd.HoaDonDate.Value.Month == dt.Month && hd.HoaDonDate.Value.Year == dt.Year)
                        HienThiHoaDon(hd);
                foreach (DataGridViewRow r in dgvListHD.Rows)
                {
                    if (r.IsNewRow)
                        continue;
                    sum += double.Parse(r.Cells[3].Value.ToString());
                }
                txtTongBan.Text = sum.ToString();
            }
        }

        private void ThongKePhieuNhap()
        {
            dgvListPhieuNhap.Rows.Clear();
            if (cbb5.Items.Count > 0 && cbb5.Text != "")
            {
                DateTime dt = DateTime.Parse(cbb5.Text);
                double sum = 0;

                foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                    if (pn.PhieuNhapDate.Value.Month == dt.Month && pn.PhieuNhapDate.Value.Year == dt.Year)
                        HienThiPhieuNhap(pn);
                foreach (DataGridViewRow r in dgvListPhieuNhap.Rows)
                {
                    if (r.IsNewRow)
                        continue;
                    sum += double.Parse(r.Cells[3].Value.ToString());
                }
                txtTongNhap.Text = sum.ToString();
            }
        }

        private void XulySachNhap(List<PhieuNhap> listPhieuNhap)
        {
            List<Sach> dsSach = new List<Sach>();
            List<ChiTietPhieuNhap> dsChiTiet = new List<ChiTietPhieuNhap>();

            foreach (PhieuNhap pn in listPhieuNhap)
                foreach (ChiTietPhieuNhap ctpn in pn.ChiTietPhieuNhaps)
                {
                    if (dsChiTiet.Count <= 0)
                        dsChiTiet.Add(ctpn);
                    else
                        if (!dsChiTiet.Contains(ctpn))
                            dsChiTiet.Add(ctpn);
                    if (dsSach.Count <= 0)
                        dsSach.Add(ctpn.Sach);
                    else
                        if (!dsSach.Contains(ctpn.Sach))
                            dsSach.Add(ctpn.Sach);
                }

            foreach (Sach s in dsSach)
            {
                int sum = 0;
                foreach (ChiTietPhieuNhap ct in dsChiTiet)
                {
                    if (ct.Sach == s)
                    {
                        sum += ct.ChiTietPhieuNhapQuantum.Value;
                    }
                }
                ChiTietPhieuNhap test = new ChiTietPhieuNhap();
                test.Sach = s;
                test.ChiTietPhieuNhapQuantum = sum;
                HienThiSachNhap(test);
            }
        }

        private void XulySachBan(List<HoaDon> listhoadon)
        {
            List<Sach> dsSach = new List<Sach>();
            List<ChiTietHoaDon> dsChiTiet = new List<ChiTietHoaDon>();

            foreach (HoaDon hd in listhoadon)
                foreach (ChiTietHoaDon cthd in hd.ChiTietHoaDons)
                {
                    if (dsChiTiet.Count <= 0)
                        dsChiTiet.Add(cthd);
                    else
                        if (!dsChiTiet.Contains(cthd))
                            dsChiTiet.Add(cthd);
                    if (dsSach.Count <= 0)
                        dsSach.Add(cthd.Sach);
                    else
                        if (!dsSach.Contains(cthd.Sach))
                            dsSach.Add(cthd.Sach);
                }

            foreach (Sach s in dsSach)
            {
                int sum = 0;
                foreach (ChiTietHoaDon ct in dsChiTiet)
                {
                    if (ct.Sach == s)
                    {
                        sum += ct.ChiTietHoaDonQuantum.Value;
                    }
                }
                ChiTietHoaDon test = new ChiTietHoaDon();
                test.Sach = s;
                test.ChiTietHoaDonQuantum = sum;
                HienThiSachBan(test);
            }
        }

        private void XulySachXuat(List<PhieuXuat> listphieuxuat)
        {
            List<Sach> dsSach = new List<Sach>();
            List<ChiTietPhieuXuat> dsChiTiet = new List<ChiTietPhieuXuat>();

            foreach (PhieuXuat px in listphieuxuat)
                foreach (ChiTietPhieuXuat ctpx in px.ChiTietPhieuXuats)
                {
                    if (dsChiTiet.Count <= 0)
                        dsChiTiet.Add(ctpx);
                    else
                        if (!dsChiTiet.Contains(ctpx))
                            dsChiTiet.Add(ctpx);
                    if (dsSach.Count <= 0)
                        dsSach.Add(ctpx.Sach);
                    else
                        if (!dsSach.Contains(ctpx.Sach))
                            dsSach.Add(ctpx.Sach);
                }

            foreach (Sach s in dsSach)
            {
                int sum = 0;
                foreach (ChiTietPhieuXuat ct in dsChiTiet)
                {
                    if (ct.Sach == s)
                    {
                        sum += ct.Quantum.Value;
                    }
                }
                ChiTietPhieuXuat test = new ChiTietPhieuXuat();
                test.Sach = s;
                test.Quantum = sum;
                HienThiSachXuat(test);
            }
        }

        private void HienThiSachNhap(ChiTietPhieuNhap ctpn)
        {
            string s = "";
            foreach (Sach_NhaCungCap sncc in ctpn.Sach.Sach_NhaCungCaps)
                s += sncc.NhaCungCap.NhaCungCapName + " ";

            int i = dgvNhapSach.Rows.Add();
            dgvNhapSach.Rows[i].Cells[0].Value = ctpn.Sach.SachNo;
            dgvNhapSach.Rows[i].Cells[1].Value = ctpn.Sach.SachName;
            dgvNhapSach.Rows[i].Cells[2].Value = s;
            dgvNhapSach.Rows[i].Cells[3].Value = ctpn.ChiTietPhieuNhapQuantum;
        }

        private void HienThiSachBan(ChiTietHoaDon cthd)
        {
            string s = "";
            foreach (Sach_NhaCungCap sncc in cthd.Sach.Sach_NhaCungCaps)
                s += sncc.NhaCungCap.NhaCungCapName + " ";

            int i = dgvBanSach.Rows.Add();
            dgvBanSach.Rows[i].Cells[0].Value = cthd.Sach.SachNo;
            dgvBanSach.Rows[i].Cells[1].Value = cthd.Sach.SachName;
            dgvBanSach.Rows[i].Cells[2].Value = s;
            dgvBanSach.Rows[i].Cells[3].Value = cthd.ChiTietHoaDonQuantum;
        }

        private void HienThiSachXuat(ChiTietPhieuXuat ctpx)
        {
            string s = "";
            foreach (Sach_NhaCungCap sncc in ctpx.Sach.Sach_NhaCungCaps)
                s += sncc.NhaCungCap.NhaCungCapName + " ";

            int i = dgvXuatSach.Rows.Add();
            dgvXuatSach.Rows[i].Cells[0].Value = ctpx.Sach.SachNo;
            dgvXuatSach.Rows[i].Cells[1].Value = ctpx.Sach.SachName;
            dgvXuatSach.Rows[i].Cells[2].Value = s;
            dgvXuatSach.Rows[i].Cells[3].Value = ctpx.Quantum;
        }

        private void HienThiHoaDon(HoaDon hd)
        {
            int sl = 0;
            double sum = 0;
            foreach (ChiTietHoaDon cthd in hd.ChiTietHoaDons)
            {
                sl += int.Parse(cthd.ChiTietHoaDonQuantum.ToString());
                sum = double.Parse(cthd.ChiTietHoaDonTotal.ToString());
            }

            int i = dgvListHD.Rows.Add();
            dgvListHD.Rows[i].Cells[0].Value = hd.HoaDonNo;
            dgvListHD.Rows[i].Cells[1].Value = hd.HoaDonDate.Value.ToShortDateString();
            dgvListHD.Rows[i].Cells[2].Value = sl;
            dgvListHD.Rows[i].Cells[3].Value = sum;
        }

        private void HienThiPhieuNhap(PhieuNhap pn)
        {
            int sl = 0;
            double sum = 0;
            foreach (ChiTietPhieuNhap ctpn in pn.ChiTietPhieuNhaps)
            {
                sl = int.Parse(ctpn.ChiTietPhieuNhapQuantum.ToString());
                sum = double.Parse(ctpn.ChiTietPhieuNhapTotal.ToString());
            }

            int i = dgvListPhieuNhap.Rows.Add();
            dgvListPhieuNhap.Rows[i].Cells[0].Value = pn.PhieuNhapNo;
            dgvListPhieuNhap.Rows[i].Cells[1].Value = pn.PhieuNhapDate.Value.ToShortDateString();
            dgvListPhieuNhap.Rows[i].Cells[2].Value = sl;
            dgvListPhieuNhap.Rows[i].Cells[3].Value = sum;
        }

        private DateTime DatetimeMin(List<DateTime> listdt)
        {
            DateTime dtmin = new DateTime();
            bool flag = false;
            foreach (DateTime dt in listdt)
            {
                if (!flag)
                {
                    dtmin = dt;
                    flag = true;
                }
                else if (dt < dtmin)
                    dtmin = dt;
            }
            return dtmin;
        }

        private DateTime DatetimeMax(List<DateTime> listdt)
        {
            DateTime dtmax = new DateTime();
            bool flag = false;
            foreach (DateTime dt in listdt)
            {
                if (!flag)
                {
                    dtmax = dt;
                    flag = true;
                }
                else if (dt < dtmax)
                    dtmax = dt;
            }
            return dtmax;
        }

        private void loadcbb1()
        {
            List<DateTime> listdt = new List<DateTime>();
            List<string> str = new List<string>();
            foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                listdt.Add(pn.PhieuNhapDate.Value);
            DateTime dtmin = DatetimeMin(listdt);
            DateTime dtmax = DatetimeMax(listdt);
            while (dtmin <= dtmax)
            {
                str.Add(dtmin.ToShortDateString().Substring(3, 7));
                dtmin = dtmin.AddMonths(1);
            }

            str.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in str)
                cbb1.Items.Add(s);

            if (cbb1.Items.Count > 0)
                cbb1.SelectedIndex = 0;
        }

        private void loadcbb2()
        {
            List<DateTime> listdt = new List<DateTime>();
            List<string> str = new List<string>();
            foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                listdt.Add(hd.HoaDonDate.Value);
            DateTime dtmin = DatetimeMin(listdt);
            DateTime dtmax = DatetimeMax(listdt);
            while (dtmin <= dtmax)
            {
                str.Add(dtmin.ToShortDateString().Substring(3, 7));
                dtmin = dtmin.AddMonths(1);
            }

            str.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in str)
                cbb2.Items.Add(s);

            if (cbb2.Items.Count > 0)
                cbb2.SelectedIndex = 0;
        }

        private void loadcbb3()
        {
            List<DateTime> listdt = new List<DateTime>();
            List<string> str = new List<string>();
            foreach (PhieuXuat px in xuly_phieuxuat.getDSPhieuXuat())
                listdt.Add(px.PhieuXuatDate.Value);
            DateTime dtmin = DatetimeMin(listdt);
            DateTime dtmax = DatetimeMax(listdt);
            while (dtmin <= dtmax)
            {
                str.Add(dtmin.ToShortDateString().Substring(3, 7));
                dtmin = dtmin.AddMonths(1);
            }

            str.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in str)
                cbb3.Items.Add(s);

            if (cbb3.Items.Count > 0)
                cbb3.SelectedIndex = 0;
        }

        private void loadcbb4()
        {
            List<DateTime> listdt = new List<DateTime>();
            List<string> str = new List<string>();
            foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                listdt.Add(hd.HoaDonDate.Value);
            DateTime dtmin = DatetimeMin(listdt);
            DateTime dtmax = DatetimeMax(listdt);
            while (dtmin <= dtmax)
            {
                str.Add(dtmin.ToShortDateString().Substring(3, 7));
                dtmin = dtmin.AddMonths(1);
            }

            str.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in str)
                cbb4.Items.Add(s);

            if (cbb4.Items.Count > 0)
                cbb4.SelectedIndex = 0;
        }

        private void loadcbb5()
        {
            List<DateTime> listdt = new List<DateTime>();
            List<string> str = new List<string>();
            foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                listdt.Add(pn.PhieuNhapDate.Value);
            DateTime dtmin = DatetimeMin(listdt);
            DateTime dtmax = DatetimeMax(listdt);
            while (dtmin <= dtmax)
            {
                str.Add(dtmin.ToShortDateString().Substring(3, 7));
                dtmin = dtmin.AddMonths(1);
            }

            str.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in str)
                cbb5.Items.Add(s);

            if (cbb5.Items.Count > 0)
                cbb5.SelectedIndex = 0;
        }

        private void loadcbbNam()
        {
            List<string> dsyear = new List<string>();

            foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                if (!dsyear.Contains(hd.HoaDonDate.Value.Year.ToString()))
                    dsyear.Add(hd.HoaDonDate.Value.Year.ToString());

            foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                if (!dsyear.Contains(pn.PhieuNhapDate.Value.Year.ToString()))
                    dsyear.Add(pn.PhieuNhapDate.Value.Year.ToString());

            dsyear.Sort(delegate(string s1, string s2)
            {
                return s1.CompareTo(s2);
            });

            foreach (string s in dsyear)
                cbbNam.Items.Add(s);

            if (cbbNam.Items.Count > 0)
                cbbNam.SelectedIndex = 0;
        }

        private void cbb1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongKeSachNhap();
        }

        private void cbb2_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongKeSachBan();
        }

        private void cbb3_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongKeSachXuat();
        }

        private void cbb4_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongKeHoaDon();
        }

        private void cbb5_SelectedIndexChanged(object sender, EventArgs e)
        {
            ThongKePhieuNhap();
        }

        private void cbbNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbNam.Text != "" && cbbNam.Items.Count > 0)
                dodulieudgv(cbbNam.Text);
        }

        private void cbb1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '/')
                e.Handled = true;

            // chỉ nhập 1 dấu /
            if (e.KeyChar == '/' && (sender as ComboBox).Text.IndexOf('/') > -1)
                e.Handled = true;
        }

        private void cbb1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (cbb1.Text != "")
                    if (kiemtradinhdang(cbb1.Text))
                        ThongKeSachNhap();
                    else
                    {
                        MessageBox.Show("Không nhập đúng định dạng");
                        cbb1.Text = "";
                    }
        }

        private void cbb1_Leave(object sender, EventArgs e)
        {
            if (cbb1.Text != "")
                if (kiemtradinhdang(cbb1.Text))
                    ThongKePhieuNhap();
                else
                {
                    MessageBox.Show("Không nhập đúng định dạng");
                    cbb1.Text = "";
                }
            else
                if (cbb1.Items.Count > 0)
                    cbb1.SelectedIndex = 0;
        }

        private void cbb2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '/')
            {
                e.Handled = true;
            }

            // chỉ nhập 1 dấu /
            if (e.KeyChar == '/'
                && (sender as ComboBox).Text.IndexOf('/') > -1)
            {
                e.Handled = true;
            }
        }

        private void cbb2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (cbb2.Text != "")
                    if (kiemtradinhdang(cbb2.Text))
                        ThongKeSachBan();
                    else
                    {
                        MessageBox.Show("Không nhập đúng định dạng");
                        cbb2.Text = "";
                    }
        }

        private void cbb3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '/')
            {
                e.Handled = true;
            }

            // chỉ nhập 1 dấu /
            if (e.KeyChar == '/'
                && (sender as ComboBox).Text.IndexOf('/') > -1)
            {
                e.Handled = true;
            }
        }

        private void cbb3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (cbb3.Text != "")
                    if (kiemtradinhdang(cbb3.Text))
                        ThongKeSachBan();
                    else
                    {
                        MessageBox.Show("Không nhập đúng định dạng");
                        cbb3.Text = "";
                    }
        }

        private void cbb4_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '/')
            {
                e.Handled = true;
            }

            // chỉ nhập 1 dấu /
            if (e.KeyChar == '/'
                && (sender as ComboBox).Text.IndexOf('/') > -1)
            {
                e.Handled = true;
            }
        }

        private void cbb4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (cbb4.Text != "")
                    if (kiemtradinhdang(cbb4.Text))
                        ThongKeHoaDon();
                    else
                    {
                        MessageBox.Show("Không nhập đúng định dạng");
                        cbb4.Text = "";
                    }
        }

        private void cbb5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '/')
            {
                e.Handled = true;
            }

            // chỉ nhập 1 dấu /
            if (e.KeyChar == '/'
                && (sender as ComboBox).Text.IndexOf('/') > -1)
            {
                e.Handled = true;
            }
        }

        private void cbb5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                if (cbb5.Text != "")
                    if (kiemtradinhdang(cbb5.Text))
                        ThongKePhieuNhap();
                    else
                    {
                        MessageBox.Show("Không nhập đúng định dạng");
                        cbb5.Text = "";
                    }
        }

        private bool kiemtradinhdang(string str)
        {
            if (cbb1.Text.First() != '/')
                if (str.Count() == 7)
                {
                    if (str.Substring(2, 1) == "/")
                        if (int.Parse(str.Substring(0, 2)) <= 12)
                            return true;
                }
                else if (str.Count() == 6)
                {
                    if (str.Substring(1, 1) == "/")
                        if (int.Parse(str.Substring(0, 1)) <= 12)
                            return true;
                }
            return false;
        }

        private void dodulieudgv(string nam)
        {
            dgvThuChi.Rows.Clear();
            int dem = 1;
            while (dem <= 12)
            {
                string s = "01/" + dem.ToString() + "/" + nam;
                DateTime dt = DateTime.Parse(s);
                int i = dgvThuChi.Rows.Add();
                dgvThuChi.Rows[i].Cells[0].Value = "Tháng " + dem;
                double thu = double.Parse(ThongKeThu(dt));
                dgvThuChi.Rows[i].Cells[1].Value = thu;
                double chi = double.Parse(ThongKeChi(dt));
                dgvThuChi.Rows[i].Cells[2].Value = chi;
                dgvThuChi.Rows[i].Cells[3].Value = thu + chi;
                dem++;
            }
            thongke();
        }

        private string ThongKeThu(DateTime dt)
        {
            double sum = 0;
            foreach (HoaDon hd in xuly_hoadon.getdsHoaDon())
                if (hd.HoaDonDate.Value.Month == dt.Month && hd.HoaDonDate.Value.Year == dt.Year)
                    foreach (ChiTietHoaDon cthd in hd.ChiTietHoaDons)
                    {
                        sum += cthd.ChiTietHoaDonTotal.Value;
                        break;
                    }
            return sum.ToString();
        }

        private string ThongKeChi(DateTime dt)
        {
            double sum = 0;
            foreach (PhieuNhap pn in xuly_phieunhap.getDsPhieuNhap())
                if (pn.PhieuNhapDate.Value.Month == dt.Month && pn.PhieuNhapDate.Value.Year == dt.Year)
                    foreach (ChiTietPhieuNhap ctpn in pn.ChiTietPhieuNhaps)
                    {
                        sum += ctpn.ChiTietPhieuNhapTotal.Value;
                        break;
                    }
            return sum.ToString();
        }

        private void thongke()
        {
            double sum = 0;
            foreach (DataGridViewRow r in dgvThuChi.Rows)
            {
                if (r.IsNewRow)
                    continue;
                double thu = double.Parse(r.Cells[1].Value.ToString());
                double chi = double.Parse(r.Cells[2].Value.ToString());
                r.Cells[3].Value = thu - chi;
                sum += double.Parse(r.Cells[3].Value.ToString());
            }
            txtLai.Text = sum.ToString();
        }

        private void dgvSachNhap_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string ma = "";
                foreach (DataGridViewRow r in dgvNhapSach.Rows)
                {
                    if (r.Index == e.RowIndex)
                        ma = r.Cells[0].Value.ToString();
                }
                if (ma != "" && cbb1.Text != "")
                {
                    FormChiTiet f = new FormChiTiet(ma, cbb1.Text, 1);
                    f.ShowDialog();
                }
            }
        }

        private void dgvBanSach_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string ma = "";
                foreach (DataGridViewRow r in dgvBanSach.Rows)
                {
                    if (r.Index == e.RowIndex)
                        ma = r.Cells[0].Value.ToString();
                }
                if(ma !=""&&cbb2.Text !="")
                {
                    FormChiTiet f = new FormChiTiet(ma,cbb2.Text,2);
                    f.ShowDialog();
                }
            }
        }

        private void dgvXuatSach_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string ma = "";
                foreach (DataGridViewRow r in dgvXuatSach.Rows)
                {
                    if (r.Index == e.RowIndex)
                        ma = r.Cells[0].Value.ToString();
                }
                if (ma != "" && cbb3.Text != "")
                {
                    FormChiTiet f = new FormChiTiet(ma, cbb3.Text, 3);
                    f.ShowDialog();
                }
            }
        }

        private void dgvListHD_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string ma = "";
                foreach (DataGridViewRow r in dgvListHD.Rows)
                {
                    if (r.Index == e.RowIndex)
                        ma = r.Cells[0].Value.ToString();
                }
                if (ma != "" && cbb4.Text != "")
                {
                    FormChiTiet f = new FormChiTiet(ma, cbb4.Text, 4);
                    f.ShowDialog();
                }
            }
        }

        private void dgvListPhieuNhap_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;

            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                string ma = "";
                foreach (DataGridViewRow r in dgvListPhieuNhap.Rows)
                {
                    if (r.Index == e.RowIndex)
                        ma = r.Cells[0].Value.ToString();
                }
                if (ma != "" && cbb5.Text != "")
                {
                    FormChiTiet f = new FormChiTiet(ma, cbb5.Text, 5);
                    f.ShowDialog();
                }
            }
        }

        private void cbb2_Leave(object sender, EventArgs e)
        {
            if (cbb2.Text != "")
                if (kiemtradinhdang(cbb2.Text))
                    ThongKePhieuNhap();
                else
                {
                    MessageBox.Show("Không nhập đúng định dạng");
                    cbb2.Text = "";
                }
            else
                if (cbb2.Items.Count > 0)
                    cbb2.SelectedIndex = 0;
        }

        private void tabControl1_Leave(object sender, EventArgs e)
        {
            if (cbb3.Text != "")
                if (kiemtradinhdang(cbb3.Text))
                    ThongKePhieuNhap();
                else
                {
                    MessageBox.Show("Không nhập đúng định dạng");
                    cbb3.Text = "";
                }
            else
                if (cbb3.Items.Count > 0)
                    cbb3.SelectedIndex = 0;
        }

        private void cbb4_Leave(object sender, EventArgs e)
        {
            if (cbb4.Text != "")
                if (kiemtradinhdang(cbb4.Text))
                    ThongKePhieuNhap();
                else
                {
                    MessageBox.Show("Không nhập đúng định dạng");
                    cbb4.Text = "";
                }
            else
                if (cbb4.Items.Count > 0)
                    cbb4.SelectedIndex = 0;
        }

        private void cbb5_Leave(object sender, EventArgs e)
        {
            if (cbb5.Text != "")
                if (kiemtradinhdang(cbb1.Text))
                    ThongKePhieuNhap();
                else
                {
                    MessageBox.Show("Không nhập đúng định dạng");
                    cbb5.Text = "";
                }
            else
                if (cbb5.Items.Count > 0)
                    cbb5.SelectedIndex = 0;
        }
    }
}
